﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Microsoft.Win32;
using System.Security.Principal;
using System.Diagnostics;

namespace Loopback_Check_Tool
{
    public partial class btn_Light : Form
    {
        public btn_Light()
        {
            InitializeComponent();
                     
            // Read current values
            RegistryKey rk = Registry.LocalMachine.CreateSubKey("SYSTEM\\CurrentControlSet\\Control\\LSA\\MSV1_0");
            string Keyname = "HKEY_LOCAL_MACHINE\\SYSTEM\\CurrentControlSet\\Control\\LSA\\MSV1_0";
            var exists = Registry.GetValue(Keyname, "BackConnectionHostNames", "0");
            if (exists != "0")
            {
                string[] urls = (string[])Registry.GetValue(Keyname, "BackConnectionHostNames", "0");
                for (int i = 0; i < urls.Length; i++)
                {
                    this.lst_SafeUrls.Items.Add(urls[i]);
                    this.lst_RegUrls.Items.Add(urls[i]);
                }
                this.ActiveControl = lbl_StatusCurrecntHeader;

            }
            // Check on/off
            string statusKeyname = "HKEY_LOCAL_MACHINE\\SYSTEM\\CurrentControlSet\\Control\\LSA";
            Int32 status = (Int32)Registry.GetValue(statusKeyname, "DisableLoopbackCheck", 0);
            if (status != 0)
            {
                // Value missing or disabled
                this.lbl_Status.Text = "Off";
                this.buttonLight.BackColor = System.Drawing.Color.IndianRed;
            }
            else
            {
                // Enabled
                this.lbl_Status.Text = "On";
                this.buttonLight.BackColor = System.Drawing.Color.LightGreen;
            }

        }

        public static bool IsAdministrator()
        {
            var identity = WindowsIdentity.GetCurrent();
            var principal = new WindowsPrincipal(identity);
            return principal.IsInRole(WindowsBuiltInRole.Administrator);
        }
        private void button_enable_Click(object sender, EventArgs e)
        {
            RegistryKey rk = Registry.LocalMachine.CreateSubKey("SYSTEM\\CurrentControlSet\\Control\\LSA");
            string statusKeyname = "HKEY_LOCAL_MACHINE\\SYSTEM\\CurrentControlSet\\Control\\LSA";
            Int32 status = (Int32)Registry.GetValue(statusKeyname, "DisableLoopbackCheck", 5);
            if (status != 5)
            {
                rk.DeleteValue("DisableLoopbackCheck");
            }
            // Enabled
            this.lbl_Status.Text = "On";
            this.buttonLight.BackColor = System.Drawing.Color.LightGreen;
            
        }

        private void button_disable_Click(object sender, EventArgs e)
        {
            RegistryKey rk = Registry.LocalMachine.CreateSubKey("SYSTEM\\CurrentControlSet\\Control\\LSA");
            rk.SetValue("DisableLoopbackCheck", "1", RegistryValueKind.DWord);

            // Value missing or disabled
            this.lbl_Status.Text = "Off";
            this.buttonLight.BackColor = System.Drawing.Color.IndianRed;

        }
        private void button_configure_Click(object sender, EventArgs e)
        {
            if (this.txt_InputBox.Text.Trim() != "")
            {
                this.lst_SafeUrls.Items.Add(this.txt_InputBox.Text);
            }
            else
            {
                MessageBox.Show("You cannot add an empty value!\r\nOnly valid NetBIOS names or FQDNs are allowed", "Loopback Check Tool", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
            }
            this.txt_InputBox.Text = "";
            
        }

        private void button_close_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            RegistryKey rk = Registry.LocalMachine.CreateSubKey("SYSTEM\\CurrentControlSet\\Control\\LSA\\MSV1_0");
            string URLsText = this.lst_SafeUrls.Text;
            if (this.lst_SafeUrls.Items.Count != 0)
            { 
                string[] LoopbackURLs = lst_SafeUrls.Items.Cast<string>().ToArray();
                //string[] LoopbackURLs = URLsText.Split(new string[] { "\n", "\r\n" }, StringSplitOptions.RemoveEmptyEntries);
                rk.SetValue("BackConnectionHostNames", LoopbackURLs, RegistryValueKind.MultiString);

                // Read current values
                string Keyname = "HKEY_LOCAL_MACHINE\\SYSTEM\\CurrentControlSet\\Control\\LSA\\MSV1_0";
                string[] urls = (string[])Registry.GetValue(Keyname, "BackConnectionHostNames", "");
            
                // Update the registry list
                this.lst_RegUrls.Items.Clear();
                for (int i = 0; i < urls.Length; i++)
                {
                    this.lst_RegUrls.Items.Add(urls[i]);
                }
                if (this.checkBox1.Checked)
                {
                    RegistryKey LSA = Registry.LocalMachine.CreateSubKey("SYSTEM\\CurrentControlSet\\Control\\LSA");
                    string statusKeyname = "HKEY_LOCAL_MACHINE\\SYSTEM\\CurrentControlSet\\Control\\LSA";
                    Int32 status = (Int32)Registry.GetValue(statusKeyname, "DisableLoopbackCheck", 5);
                    if (status != 5)
                    {
                        LSA.DeleteValue("DisableLoopbackCheck");
                    }
                    // Enabled
                    this.lbl_Status.Text = "On";
                    this.buttonLight.BackColor = System.Drawing.Color.LightGreen;
                }
            }
            else
            {
                RegistryKey LSA = Registry.LocalMachine.CreateSubKey("SYSTEM\\CurrentControlSet\\Control\\LSA\\MSV1_0");
                string statusKeyname = "HKEY_LOCAL_MACHINE\\SYSTEM\\CurrentControlSet\\Control\\LSA\\MSV1_0";
                String[] status = (String[])Registry.GetValue(statusKeyname, "BackConnectionHostNames", new string[] {""} );
                if (status[0] != "")
                {
                    LSA.DeleteValue("BackConnectionHostNames");
                    this.lst_RegUrls.Items.Clear();
                }
            }
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void button6_Click(object sender, EventArgs e)
        {
            if (this.lst_SafeUrls.SelectedIndex >= 0)
            {
                this.lst_SafeUrls.Items.RemoveAt(this.lst_SafeUrls.SelectedIndex);
            }

        }

        private void button5_Click(object sender, EventArgs e)
        {
            // Save latest entrys
            RegistryKey rk = Registry.LocalMachine.CreateSubKey("SYSTEM\\CurrentControlSet\\Control\\LSA\\MSV1_0");
            string URLsText = this.lst_SafeUrls.Text;
            string[] LoopbackURLs = lst_SafeUrls.Items.Cast<string>().ToArray();
            //string[] LoopbackURLs = URLsText.Split(new string[] { "\n", "\r\n" }, StringSplitOptions.RemoveEmptyEntries);
            rk.SetValue("BackConnectionHostNames", LoopbackURLs, RegistryValueKind.MultiString);

            // CLose application
            Application.Exit();

        }

        private void label2_Click(object sender, EventArgs e)
        {

        }
    }
}
